#include "StdAfx.h"
#include "Utility.h"

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <winnls.h>


CUtility::CUtility(void)
{
}

CUtility::~CUtility(void)
{
}

void CUtility::UpdateWindow(HWND hWnd, HDC hdc, CRect rect)
{
	HMODULE hFuncInst = LoadLibrary(_T("User32.DLL"));
    typedef BOOL (WINAPI *MYFUNC)(HWND, HDC, POINT*, SIZE*, HDC, 
							  POINT*, COLORREF, BLENDFUNCTION*, DWORD);         
	MYFUNC UpdateLayeredWindow = (MYFUNC)::GetProcAddress(hFuncInst, "UpdateLayeredWindow");
	
	BLENDFUNCTION blend;
	blend.AlphaFormat = 1;
	blend.BlendFlags = 0;
	blend.BlendOp = AC_SRC_OVER;
	blend.SourceConstantAlpha = 255;

	if ( UpdateLayeredWindow )
	{
		POINT ptSrc = {0, 0};
		POINT ptDrc = {0, 0};
		CSize size(rect.Width(), rect.Height());
		HDC hwndDC = ::GetDC( hWnd );
		UpdateLayeredWindow(hWnd, hwndDC, 0, &size, hdc, &ptSrc, 0, &blend, 2);
		::ReleaseDC(hWnd, hwndDC);
	}
	::FreeLibrary( hFuncInst );
}


BOOL CUtility::ImageFromIDResource(UINT nID, LPCTSTR sTR, Image * &pImg)
{
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
	if (!hRsrc)
		return FALSE;
	// load resource into memory
	DWORD len = SizeofResource(hInst, hRsrc);
	BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
	if (!lpRsrc)
		return FALSE;
	// Allocate global memory on which to create stream
	HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
	BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
	memcpy(pmem,lpRsrc,len);
	IStream* pstm;
	CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
	// load from stream
	pImg=Gdiplus::Image::FromStream(pstm);
	// free/release stuff
	GlobalUnlock(m_hMem);
	pstm->Release();
	FreeResource(lpRsrc);
	return TRUE;
}

BOOL CUtility::StringFromIDResource(UINT nID, LPCTSTR sTR, CString& str)
{
    str.Empty();
	HINSTANCE hInst=AfxGetResourceHandle();
	HRSRC hRsrc=::FindResource(hInst,MAKEINTRESOURCE(nID),sTR);   
	if (hRsrc==NULL)
		return FALSE;
	
	DWORD dwLen=SizeofResource(hInst,hRsrc);
	HGLOBAL hGlobal =NULL;
	hGlobal=LoadResource(hInst,hRsrc);
	if (hGlobal==NULL)
	{
		FreeResource(hRsrc);
		return FALSE;
	}
    LPVOID   pData = NULL;
	pData = LockResource(hGlobal);      
    if(pData ==NULL)     
    {     
         FreeResource(hRsrc);
		 return   FALSE;     
	}
	
	size_t stLen = (dwLen * sizeof(BYTE))/sizeof(TCHAR) ;

	TCHAR* pByte = new TCHAR[ stLen +1];
	memset(pByte, 0, (stLen+1)*sizeof(TCHAR) );

	TCHAR* pDataTemp = NULL;
    TCHAR fileType;
    fileType = *((TCHAR *)pData);
    //remove the unicode file header
    if (fileType == 0xfffe || fileType == 0xfeff )
    {
        pDataTemp = ((TCHAR *)pData) +1;
        stLen--;
    }
    else
    {
        pDataTemp = (TCHAR *)pData;
    }

	TCHAR* pByteTemp = pByte;
	
	while (stLen--)
	{
		*pByteTemp++ = *pDataTemp++ ;
	}

	*pByteTemp = _T('\0');
	
	str = pByte;
	

	delete []pByte;
	pByte = pByteTemp =NULL;


	UnlockResource(hGlobal);
	pDataTemp = NULL;
	pData =NULL;

	FreeResource(hRsrc);
	return TRUE;     
}
